package com.tattermedia.allthataudio.activities;

import hw.ats.news.R;
import hw.ats.news.activity.DBAwareActivity;
import hw.ats.news.activity.news.ViewAct;
import hw.ats.news.ui.BottomTabHostProvider;
import hw.ats.news.ui.CommonUI;
import hw.ats.news.util.App;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.humanwares.ui.tabbar.TabHost;
import com.tattermedia.allthataudio.audio.AudioPlayerDialogTool;
import com.tattermedia.allthataudio.audio.AudioPlayerManager;
import com.tattermedia.allthataudio.audio.CommonAudioPlayerStatusListener;
import com.tattermedia.allthataudio.audio.IAudioPlayerStatusListener;
import com.tattermedia.allthataudio.audio.PlayListItem;
import com.tattermedia.allthataudio.views.AudioPlayerProgressView;

/**
 * Play List Activity
 */
public class PlayListActivity extends DBAwareActivity implements IAudioPlayerStatusListener {
	
	private TabHost 		mNaviBar;
	private ListView		mPlayListView;
	
	// edit flag
	private boolean 		mIsEdit;
	
	// audio status listener
	private IAudioPlayerStatusListener 	mAudioStatusListener;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
        setContentView(R.layout.act_playlist);
        
        // init tabbar
        mNaviBar = CommonUI.renderTabBar(this, BottomTabHostProvider.TAB2);
        
		// navigation bar title
        CommonUI.renderAppTitle(this);
        
        // get playlist view
        mPlayListView = (ListView)findViewById(R.id.playlist_view);
        mPlayListView.setAdapter(new PlayListAdapter(this));
        mPlayListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				try {
					AudioPlayerManager.getManager().playAudioFromIndex(position);
				} catch (Exception e) {
				}
			}
        });
        
        // init edit button
        ImageButton editBookmark = (ImageButton)findViewById(R.id.navbar_right);
        editBookmark.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mIsEdit = !mIsEdit;
				((BaseAdapter)mPlayListView.getAdapter()).notifyDataSetChanged();
			}
		});
        
        // add audio status listener
        mAudioStatusListener = new CommonAudioPlayerStatusListener(
        		(ImageButton)findViewById(R.id.audio_player_button_play), 
        		(ImageButton)findViewById(R.id.audio_player_button_repeat),
        		(ImageButton)findViewById(R.id.audio_player_button_prev),
        		(ImageButton)findViewById(R.id.audio_player_button_next),
        		(TextView)findViewById(R.id.audio_player_text_subject),
        		(TextView)findViewById(R.id.audio_player_text_time),
        		(AudioPlayerProgressView)findViewById(R.id.audio_player_progress_view));
        
        AudioPlayerManager.getManager().addAudioPlayerStatusListener(mAudioStatusListener);
        AudioPlayerManager.getManager().addAudioPlayerStatusListener(this);
        
        mAudioStatusListener.onAudioStatusChanged(IAudioPlayerStatusListener.TYPE_COMMON);        
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		AudioPlayerManager.getManager().removeAudioPlayerStatusListener(mAudioStatusListener);
		AudioPlayerManager.getManager().removeAudioPlayerStatusListener(this);
		
		closeDatabase();
	}
	
	@Override
	protected void onRestart() {
		super.onRestart();
		
		((BaseAdapter)mPlayListView.getAdapter()).notifyDataSetChanged();			
	}
	
	protected class PlayListAdapter extends BaseAdapter {
	   	private Activity 	mContext;

        public PlayListAdapter(Activity context) {
        	this.mContext = context;
        }

        public int getCount() {
        	return AudioPlayerManager.getManager().getPlayListItemCount();
        }

        public Object getItem(int position) {
        	return AudioPlayerManager.getManager().getPlayListItemFromIndex(position);
        }

        public long getItemId(int position) {
            return position;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;

            PlayListItemHolder holder = null;
            
            if (view == null || view.getTag() == null) {
            	LayoutInflater vi = mContext.getLayoutInflater();
            	
            	view = vi.inflate(R.layout.row_playlist, null);
            	
                holder = new PlayListItemHolder(view);
                view.setTag(holder);
            } else {
            	holder = (PlayListItemHolder)view.getTag();
            }

            final PlayListItem item = (PlayListItem)getItem(position);
    		if (item == null) {
    			return view;
    		}
    		
    		if (holder.mTxTitle != null) {
    			holder.mTxTitle.setText(item.getTitle());
    		}
            
    		
    		if (mIsEdit) {
    			// edit mode
    			holder.changeToEditMode(PlayListActivity.this, item, position);
    			
    			// set delete button click listener
    			holder.mBtDelete.setOnClickListener(new OnClickListener() {
    				
    				@Override
    				public void onClick(View v) {
    					
    					AudioPlayerDialogTool.showPlayListRemoveItemDialog(PlayListActivity.this, item, (BaseAdapter)mPlayListView.getAdapter());
 
    				}
    			});
    			
    			// set up button click listener
    			holder.mBtArrowUp.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						AudioPlayerManager.getManager().moveUp(item);	
						
						((BaseAdapter)mPlayListView.getAdapter()).notifyDataSetChanged();
					}
				});
    			
    			// set down button click listener
    			holder.mBtArrowDown.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						AudioPlayerManager.getManager().moveDown(item);		
						
						((BaseAdapter)mPlayListView.getAdapter()).notifyDataSetChanged();
						
					}
				});
    			
    			
    		} else {
    			// nomal mode
    			holder.changeToNomalMode(PlayListActivity.this, item, position);
    			
    			// set view button click listener
    			holder.mBtView.setOnClickListener(new OnClickListener() {
    				
    				@Override
    				public void onClick(View v) {

    					Intent intent = new Intent(PlayListActivity.this, ViewAct.class);
    					intent.putExtra(App.INTENT_EXTRA_NEWS_UID, item.getUid());
    					intent.putExtra(App.INTENT_EXTRA_CATE_UID, 0);
    					intent.putExtra(App.INTENT_EXTRA_ACT_NAME, PlayListActivity.class.getName());
    				    startActivity(intent);
    					
    				}
    			});
    		}            
    		
    		return view;
        }
	}
	
	protected class PlayListItemHolder {
		public View			mView;
		public ImageView	mImgView;
		public TextView		mTxTitle;
		public ImageButton	mBtArrowUp;
		public ImageButton	mBtArrowDown;
		public ImageButton	mBtView;
		public ImageButton	mBtDelete;
		
		public PlayListItemHolder(View view) {
			mView = view;
			mImgView = (ImageView)view.findViewById(R.id.row_image_view);
			mTxTitle = (TextView)view.findViewById(R.id.row_title);
			mBtArrowUp = (ImageButton)view.findViewById(R.id.row_button_arrow_up);
			mBtArrowDown = (ImageButton)view.findViewById(R.id.row_button_arrow_down);
			mBtView = (ImageButton)view.findViewById(R.id.row_button_view);
			mBtDelete = (ImageButton)view.findViewById(R.id.row_button_delete);
		}
		
		public void changeToEditMode(final Context context, final PlayListItem item, final int position) {
			mImgView.setVisibility(View.GONE);
			mBtView.setVisibility(View.GONE);
			
			mBtArrowUp.setVisibility(View.VISIBLE);
			mBtArrowDown.setVisibility(View.VISIBLE);
			mBtDelete.setVisibility(View.VISIBLE);
			
			mBtView.setFocusable(false);
			mBtArrowUp.setFocusable(false);
			mBtArrowDown.setFocusable(false);
			mBtDelete.setFocusable(false);
			
			if (position == 0) {
				mBtArrowUp.setEnabled(false);
			} else {
				mBtArrowUp.setEnabled(true);
			}
			
			if (position == AudioPlayerManager.getManager().getPlayListItemCount() - 1) {
				mBtArrowDown.setEnabled(false);
			} else {
				mBtArrowDown.setEnabled(true);
			}
			
			// play status image
			PlayListItem currentItem = AudioPlayerManager.getManager().getCurrentPlayingItem();
			
			if (currentItem == item) {
				mImgView.setImageResource(R.drawable.audio_playlist_play);
				mView.setBackgroundColor(Color.rgb(0xee, 0xee, 0xee));
			} else {
				mImgView.setImageResource(R.drawable.audio_playlist_song);
				mView.setBackgroundResource(R.drawable.row_news_background);
			}	

		}
		
		public void changeToNomalMode(final Context context, final PlayListItem item, final int position) {
			mBtArrowUp.setVisibility(View.GONE);
			mBtArrowDown.setVisibility(View.GONE);
			mBtDelete.setVisibility(View.GONE);
			
			mImgView.setVisibility(View.VISIBLE);
			mBtView.setVisibility(View.VISIBLE);
			
			mBtView.setFocusable(false);
			mBtArrowUp.setFocusable(false);
			mBtArrowDown.setFocusable(false);
			mBtDelete.setFocusable(false);
			
			// play status image
			PlayListItem currentItem = AudioPlayerManager.getManager().getCurrentPlayingItem();
			
			if (currentItem == item) {
				mImgView.setImageResource(R.drawable.audio_playlist_play);
				mView.setBackgroundColor(Color.rgb(0xee, 0xee, 0xee));
			} else {
				mImgView.setImageResource(R.drawable.audio_playlist_song);
				mView.setBackgroundResource(R.drawable.row_news_background);
			}			
		}
				
	}

	@Override
	public void onAudioStatusChanged(int type) {
		if (type == IAudioPlayerStatusListener.TYPE_START_PLAY) {
			((BaseAdapter)mPlayListView.getAdapter()).notifyDataSetChanged();
		}
	}

	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        int visibility = mNaviBar.getVisibility();
        mNaviBar.setVisibility(visibility == View.VISIBLE ? View.INVISIBLE : View.VISIBLE);
        return false;
    }

}
